package com.yanqu.road.web.mgr;

import com.yanqu.road.entity.enums.eSexType;
import com.yanqu.road.entity.language.LanguageType;
import com.yanqu.road.server.manger.NameMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;
import com.yanqu.road.webservice.invoke.GameWebServiceInvoke;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class TempNameMgr {

    private static RandomHelper random = new RandomHelper();

    public static String getRandomName(eSexType sex, String language) {
        Map<String, List<String>> surNameMap = NameMgr.getSurNameMap();

        Map<String, List<String>> nameMap = NameMgr.getNameMap();

        Map<String, List<String>> nameWMap = NameMgr.getNameWMap();

        String randomName;
        String surName = "";
        String name = "";
        List<String> surNameList;
        List<String> nameList;
        surNameList = surNameMap.get(language);
        if(null == surNameList){
            surNameList = surNameMap.get(LanguageType.LANGUAGE_DEFAULT);
        }
        switch (sex){
            case MALE:
                nameList = nameMap.get(language);
                if(null == nameList){
                    nameList = nameMap.get(LanguageType.LANGUAGE_DEFAULT);
                }
                break;
            case FEMALE:
                nameList = nameWMap.get(language);
                if(null == nameList){
                    nameList = nameWMap.get(LanguageType.LANGUAGE_DEFAULT);
                }
                break;
            default:
                return "";
        }

        boolean isForbid;
        int loopTimes = 0;
        do {
            isForbid = false;
            if (null != surNameList) {
                surName = surNameList.get(random.next(0, surNameList.size()));
            }

            if (null != nameList) {
                if (NameMgr.isSingleName()) {
                    name = nameList.get(random.next(0, nameList.size()));
                } else {
                    name = nameList.get(random.next(0, nameList.size())) + nameList.get(random.next(0, nameList.size()));
                }
            }
            randomName = surName + name;
            if(!StringUtils.isNullOrEmpty(randomName)) {
                if(Objects.equals(LanguageType.LANGUAGE_RU_RU, language)){//俄语诡异的取名规则，名字+#123
                    randomName = randomName + "#" + NameMgr.randomStr();
                }
                if (GameWebServiceInvoke.checkNameForbidden(randomName)) {
                    isForbid = true;
                }
            }
            loopTimes++;
            if(loopTimes > 30){
                break;
            }
        }while (isForbid);
        return randomName;
    }
}
